﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BL.Model;
using BL.Model.Request;
using DAL;
using DAL.Model;

namespace BL
{
    /// <summary>
    /// Controller for product category
    /// </summary>
    /// <history>
    /// 11/30/2014 phat.vo: Create new
    /// </history>
    public class ProductCategoryController : BaseController, IProductCategoryController
    {
        readonly IRepository<ProductCategory> _repository;
        private readonly IDbContext _dbContext;

        #region Ctor

        public ProductCategoryController()
        {
            _dbContext = new CustomObjectContext(NameOrConnectionString);
            _repository = new EfRepository<ProductCategory>(_dbContext);
        }

        public ProductCategoryController(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
            _dbContext = new CustomObjectContext(NameOrConnectionString);
            _repository = new EfRepository<ProductCategory>(_dbContext);
        }

        #endregion

        public void Create()
        {
            throw new NotImplementedException();
        }

        public void View()
        {
            throw new NotImplementedException();
        }

        public void Update()
        {
            throw new NotImplementedException();
        }

        public void Delete()
        {
            throw new NotImplementedException();
        }

        public void Print()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Create new product category
        /// </summary>
        /// <param name="item"></param>
        /// <history>
        /// 11/30/2014 phat.vo: Create new
        /// </history>
        public void Create(ProductCategory item)
        {
            _repository.Insert(item);
            _dbContext.SaveChanges();
        }

        /// <summary>
        /// Update product category name, code
        /// </summary>
        /// <param name="item"></param>
        /// <history>
        /// 11/30/2014 phat.vo: Create new
        /// </history>
        public void Update(ProductCategory item)
        {
            var oldItem = _repository.GetById(item.Id);
            if (oldItem != null)
            {
                oldItem.Name = item.Name;
                oldItem.Code = item.Code;
                _repository.Update(item);
                _dbContext.SaveChanges();
            }
        }


        /// <summary>
        /// Delete product category
        /// </summary>
        /// <param name="item"></param>
        /// <history>
        /// 11/30/2014 phat.vo: Create new
        /// </history>
        public void Delete(ProductCategory item)
        {
            _repository.Delete(item);
            _dbContext.SaveChanges();
        }


        /// <summary>
        /// Search product category by keyword with paging and ordering
        /// </summary>
        /// <param name="request"></param>
        /// <returns>List of product category</returns>
        /// <history>
        /// 11/30/2014 phat.vo: Create new
        /// </history>
        public List<ProductCategory> Search(SearchingRequest request)
        {
            var isAllKeyword = string.IsNullOrWhiteSpace(request.Keyword);
            var keyword = isAllKeyword ? string.Empty : request.Keyword.Trim().ToLower();
            var query = from x in _repository.Table
                        where (isAllKeyword ||
                               x.Name.ToLower().Contains(keyword) ||
                               x.Code.ToLower().Contains(keyword))
                        select x;

            if (request.IsPaging)
            {
                if (request.PageLength < 0)
                {
                    throw new Exception("PageLenght must be greater than 0");
                }
                if (request.StartIndex < 0)
                {
                    throw new Exception("StartIndex must be greater than 0");
                }
                query = query.OrderBy(x => x.Name).Skip(request.StartIndex).Take(request.PageLength);
            }

            return query.ToList();
        }
    }
}
